package com.tushen.labor.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {


    public static  String getNowWeekStr(){

        String pat = "yyyy-MM-dd HH:mm:ss EEEE";
        SimpleDateFormat format = new SimpleDateFormat(pat);
        Calendar cal = Calendar.getInstance();
        Date now = cal.getTime();
        try {
            return format.format(now);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public static  String getDateZeroStr(){

        String pat = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat format = new SimpleDateFormat(pat);
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        try {
            return format.format(cal.getTime());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public static  String getNowDateStr(){

        String pat = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat format = new SimpleDateFormat(pat);
        Calendar cal = Calendar.getInstance();
        Date now = cal.getTime();
        try {
            return format.format(now);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public static  String getNowHHmmssStr(){

        String pat = "HH:mm:ss";
        SimpleDateFormat format = new SimpleDateFormat(pat);
        Calendar cal = Calendar.getInstance();
        Date now = cal.getTime();
        try {
            return format.format(now);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public static  String getNowHHStr(){
        String pat = "HH";
        SimpleDateFormat format = new SimpleDateFormat(pat);
        Calendar cal = Calendar.getInstance();
        Date now = cal.getTime();
        try {
            return format.format(now);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }


    public static  String getYYYYMMDDStr(){

        String pat = "yyyy-MM-dd";
        SimpleDateFormat format = new SimpleDateFormat(pat);
        Calendar cal = Calendar.getInstance();
        Date now = cal.getTime();
        try {
            return format.format(now);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    //偏移天数
    public static  String getYYYYMMDDStr(int offsetDays){

        String pat = "yyyy-MM-dd";
        SimpleDateFormat format = new SimpleDateFormat(pat);
        Calendar cal = Calendar.getInstance();
        Date now = cal.getTime();
        cal.add(Calendar.DAY_OF_MONTH,offsetDays);
        try {
            return format.format(cal.getTime());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public static  String getYYYYStr(){

        String pat = "yyyy";
        SimpleDateFormat format = new SimpleDateFormat(pat);
        Calendar cal = Calendar.getInstance();
        Date now = cal.getTime();
        try {
            return format.format(now);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public static  String getMMDDStr(){

        String pat = "MM月dd日";
        SimpleDateFormat format = new SimpleDateFormat(pat);
        Calendar cal = Calendar.getInstance();
        Date now = cal.getTime();
        try {
            return format.format(now);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 获取当前日期是 周几
     * @return
     */
    public static String getNowWeekOfDate() {
        String[] weekDays = { "周日", "周一", "周二", "周三", "周四", "周五", "周六" };
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return weekDays[w];
    }

    public static String getWeekOfDate(int offsetDays) {
        String[] weekDays = { "周日", "周一", "周二", "周三", "周四", "周五", "周六" };
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.DAY_OF_MONTH,offsetDays);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return weekDays[w];
    }

    public static ThreadLocal<SimpleDateFormat> timeFormat = new ThreadLocal<SimpleDateFormat>() {
        @Override
        protected SimpleDateFormat initialValue() {
            return new SimpleDateFormat("HH:mm:ss");
        }
    };

    public static String formatTime(long millis) {
        return timeFormat.get().format(new Date(millis));
    }

    public static  String getNowDDStr(){
        String pat = "dd";
        SimpleDateFormat format = new SimpleDateFormat(pat);
        Calendar cal = Calendar.getInstance();
        Date now = cal.getTime();
        try {
            return format.format(now);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public static  String getNowmmStr(){
        String pat = "mm";
        SimpleDateFormat format = new SimpleDateFormat(pat);
        Calendar cal = Calendar.getInstance();
        Date now = cal.getTime();
        try {
            return format.format(now);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

}
